<?php 
class Bbling_Model_Resource_Abstract extends Evebit_Model_Mysql_Abstract {
    
	protected function _construct() {
    	
	}
	
    public function getTableData($object) {
		$table = $this->getMainTable ();
		$datas = parent::getTableData ( $table, $object );
		
		return $datas;
	}
	
	public function extraSave($object) {
		$datas = $this->getTableData ( $object );
		$db = $this->getReadConnection ();
		$id = null;
		if ($object->id) {
			$businessEntity = new Bbling_Model_Entity_Business ();
			$id = $businessEntity->getTableId($object->id, $this->getMainTable());
			if (count($datas)){
				$result = $db->update ( $this->getMainTable (), $datas, 'id = ' . $id );
			}else{
				$result = $id;
			}
			
		} else {
			$tableStatus = $db->fetchRow ( "SHOW TABLE STATUS LIKE " . $db->quote ( $this->getMainTable () ) );
			$id = $tableStatus ['Auto_increment'];
		    $result = $db->insert ( $this->getMainTable (), $datas );
		}
		
		return $result?$id:false;
	}
	
	public function extraLoad($object) {
		$db = $this->getReadConnection ();
		$select = $db->select ()->from ( $this->getMainTable () )->where ( "id=?", $object->id );
		$result = $db->fetchRow ( $select );
		
		return $result;
	}
	
	public function extraDelete($object){
		$db = $this->getReadConnection ();
		$db->delete($this->getMainTable(), $db->quoteInto ( 'id = ?', $object->id ));
		
		return $object;
	}
	
    public function getTableFields($hasId = false){
		$db = $this->getReadConnection ();
		$fields = $db->describeTable($this->getMainTable());
		$fields = array_keys($fields);
		if ($fields[0] == 'id' && !$hasId){
			array_shift($fields);
		}
		return $fields;
	}
}